package com.collect.senegal.zhiGlobal.zhiHandler;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.collect.senegal.zhiUtils.StringUtil;

public class ZHIProgressDialogHandler extends Handler {

    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;


    protected KProgressHUD progressHUD;

    private Context context;
    private boolean cancelable;
    private KProgressHUD.Style style;
    private String label;
    private ProgressCancelListener mProgressCancelListener;

    public ZHIProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener,
                                    boolean cancelable) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
    }

    public ZHIProgressDialogHandler(Context context, String label) {
        super();
        this.context = context;
        this.mProgressCancelListener = null;
        this.cancelable = false;
        this.label = label;
    }

    public void setLabel(String label){
        this.label = label;
        if (progressHUD != null) {
            progressHUD.setLabel(label);
        }
    }
    private void initProgressDialog(){
        if (progressHUD == null){
            progressHUD = KProgressHUD.create(context).setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
            progressHUD.setAnimationSpeed(2)
                    .setDimAmount(0.5f)
                    .setSize(100, 100);
        }

        if (!StringUtil.isEmpty(this.label)){
            progressHUD.setLabel(this.label);
        }

        try {
            progressHUD.show();
        }
        catch (Exception e){

        }

    }

    private void dismissProgressDialog(){
        if (progressHUD != null) {
            progressHUD.dismiss();
            progressHUD = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }


    public interface ProgressCancelListener {
        void onCancelProgress();
    }

    public void show(){
        this.obtainMessage(ZHIProgressDialogHandler.SHOW_PROGRESS_DIALOG)
                .sendToTarget();
    }
    public void hide(){
        this.obtainMessage(ZHIProgressDialogHandler.DISMISS_PROGRESS_DIALOG)
                .sendToTarget();
    }

    public void destroy(){

        dismissProgressDialog();
    }
}